package com.jincp.filepicker.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.R;
import com.jincp.filepicker.bean.VideoFile;
import com.jincp.filepicker.util.MediaFileUtil;
import com.jincp.filepicker.ui.video.VideoPickerManager;
import com.jincp.image.load.ImageLoader;

/**
 * @ProjectName: filepicker.
 * @ClassName: ImagePickAdapter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/3/22 21:44.
 */
public class VideoPickAdapter extends BaseQuickAdapter<VideoFile, BaseViewHolder> {

    private int currentSelectedCount = 0;

    public VideoPickAdapter() {
        super(R.layout.item_video_pick);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final VideoFile item) {
        if (item == null) {
            holder.setVisible(R.id.iv_camera, true);
            holder.setVisible(R.id.iv_thumbnail, false);
            holder.setVisible(R.id.layout_duration, false);
            holder.setVisible(R.id.iv_cbx, false);
        } else {
            ImageLoader.getInstance().displayImage(item.getPath(), (ImageView) holder.getView(R.id.iv_thumbnail));
            holder.setVisible(R.id.iv_camera, false);
            holder.setVisible(R.id.iv_thumbnail, true);

            holder.setVisible(R.id.iv_cbx, VideoPickerManager.getInstance().isMultiMode());
            holder.setSelected(R.id.iv_cbx, VideoPickerManager.getInstance().isContainSelected(item));
            holder.getView(R.id.iv_cbx).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    doSelectedImage(item, holder);
                }
            });

            holder.setVisible(R.id.layout_duration, true);
            holder.setText(R.id.tv_duration, MediaFileUtil.getDurationString(item.getDuration()));
            if (VideoPickerManager.getInstance().isContainSelected(item)) {
                ((ImageView) holder.getView(R.id.iv_thumbnail)).setColorFilter(Color.parseColor("#80000000"));
            } else {
                ((ImageView) holder.getView(R.id.iv_thumbnail)).clearColorFilter();
            }

        }
    }

    private void doSelectedImage(VideoFile item, BaseViewHolder holder) {
        if (!VideoPickerManager.getInstance().isContainSelected(item) && isOverMaxSelectedLimit()) {
            ToastUtil.getInstance().show("已达到选择上限");
            return;
        }
        if (VideoPickerManager.getInstance().isContainSelected(item)) {
            VideoPickerManager.getInstance().removeSelectedFile(item);
            holder.setSelected(R.id.iv_cbx, false);
            ((ImageView) holder.getView(R.id.iv_thumbnail)).clearColorFilter();
            currentSelectedCount--;
        } else {
            VideoPickerManager.getInstance().addSelectedFile(item);
            holder.setSelected(R.id.iv_cbx, true);
            ((ImageView) holder.getView(R.id.iv_thumbnail)).setColorFilter(Color.parseColor("#80000000"));
            currentSelectedCount++;
        }
    }

    public boolean isOverMaxSelectedLimit() {
        return currentSelectedCount >= VideoPickerManager.getInstance().getMaxSelectedLimit();
    }
}
